<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class user_mod extends module
{
    function get_one($uid)
    {
        return $this->db->get_one("select * from " . table("member") . " where uid = " . $uid);
    }
    function userInfo($username)
    {
        return $this->db->get_one("select * from " . table("member") . " where username = '" . $username . "' limit 1");
    }
    function now_user()
    {
        $uid = $_SESSION['user']['uid'];
        return $this->db->get_one("select * from " . table("member") . " where uid = " . $uid);
    }
    function friend_relate($insert_uid)
    {
        $from_uid = @$_SESSION['distribute']['uid'];
        if ($from_uid) {
            $query = $this->db->add(table('distribute'), array('uid' => $insert_uid, 'from_uid' => $from_uid));
            if ($query) {
                if ($from_uid) {
                    $distribute_num = $this->db->get_one("select count(id) as total from " . table('distribute_rate'));
                    if ($distribute_num['total']) {
                        for ($i = 1; $i <= $distribute_num['total']; $i++) {
                            $from_arr['uid'] = $from_uid;
                            $from_arr['child_uid'] = $insert_uid;
                            $from_arr['level'] = $i;
                            $this->db->add(table('distribute_relate'), $from_arr);
                            $urow = $this->db->get_one("select uid,from_uid from " . table('distribute') . " where uid = " . $from_uid);
                            if ($urow['uid']) {
                                if ($urow['from_uid'] != '0') {
                                    $from_uid = $urow['from_uid'];
                                } else {
                                    break;
                                }
                            } else {
                                break;
                            }
                        }
                    }
                }
            }
        } else {
            $query = $this->db->add(table('distribute'), array('uid' => $insert_uid));
        }
    }
    function check_user($username)
    {
        if (verify::is_email($username)) {
            $row = $this->db->get_one("select uid,email,password,rand,refresh_time from " . table("member") . " where email='" . $username . "'");
        } elseif (verify::is_mobile($username)) {
            $row = $this->db->get_one("select uid,mobile,password,rand,refresh_time from " . table("member") . " where mobile='" . $username . "'");
        } else {
            $row = $this->db->get_one("select uid,username,password,rand,refresh_time from " . table("member") . " where username='" . $username . "'");
        }
        return $row;
    }
    function user_session($row)
    {
        unset($_SESSION['user']);
        unset($_SESSION['shop']);
        $urow['uid'] = $row['uid'];
        $urow['type'] = $row['type'];
        $urow['user_status'] = $row['status'];
        $urow['username'] = $row['username'];
        $urow['nick'] = $row['nick'];
        $urow['email'] = hideEmail($row['email']);
        $urow['mobile'] = hideMobile($row['mobile']);
        $urow['avatar'] = $row['avatar'];
        $_SESSION['user'] = $urow;
        return true;
    }
    function create_shop_session($sp_row)
    {
        unset($_SESSION['user']);
        unset($_SESSION['shop']);
        $sprow['spid'] = $sp_row['id'];
        $sprow['type'] = $sp_row['type'];
        $sprow['name'] = $sp_row['name'];
        $sprow['logo'] = $sp_row['logo'];
        $sprow['status'] = $sp_row['status'];
        $_SESSION['shop'] = $sprow;
    }
    function send_register_sms($mobile, $vcode, $check_status = '1')
    {
        if (strtolower($_SESSION['vcode']) == strtolower($vcode)) {
            if (verify::is_mobile($mobile)) {
                if ($check_status == '1') {
                    $row = $this->check_user($mobile);
                    if (!$row['uid']) {
                        error_json('该用户不存在');
                    }
                } elseif ($check_status == '2') {
                    $row = $this->check_user($mobile);
                    if ($row['uid']) {
                        error_json('该用户已经存在');
                    }
                }
                $code = rand_number('6');
                $use_status = true;
                if ($use_status) {
                    $param['smscode'] = $code;
                    $backarr = $this->module('api')->notice_sms($mobile, 'vcode', $param);
                    if ($backarr['error'] == '0') {
                        @($_SESSION['sms_code'] = $code);
                        @($_SESSION['sms_mobile'] = $mobile);
                        $result['error'] = '0';
                        $result['msg'] = '发送成功';
                        echo json_encode($result);
                    } else {
                        error_json($backarr['msg']);
                    }
                } else {
                    file_put_contents(UQCMS_PATH . 'temp/sms_code.txt', $code);
                    @($_SESSION['sms_code'] = $code);
                    @($_SESSION['sms_mobile'] = $mobile);
                    $result['error'] = '0';
                    $result['msg'] = '发送成功';
                    echo json_encode($result);
                }
            } else {
                error_json('输入的手机格式不正确');
            }
        } else {
            error_json('图形验证码错误');
        }
    }
    function forget_check($username, $sms_code)
    {
        if ($username && verify::is_mobile($username)) {
            if (trim($sms_code) == $_SESSION['sms_code']) {
                if (trim($username) == $_SESSION['sms_mobile']) {
                    $row = $this->check_user($username);
                    if ($row['uid']) {
                        $this->unset_mobile();
                        $query = $this->db->update(table('member'), array('refresh_time' => time()), 'uid = ' . $row['uid']);
                        if ($query) {
                            $access_token = md5($row['uid'] . $username . $row['password']);
                            @($_SESSION['mobile_update_token'] = base64_encode($username . '|' . $access_token));
                            $result['error'] = '0';
                            $result['msg'] = '验证成功';
                            echo json_encode($result);
                        } else {
                            error_json('提交错误，请稍后再试。');
                        }
                    } else {
                        error_json('电话号码不存在');
                    }
                } else {
                    error_json('提交的号码和验证的号码不一致');
                }
            } else {
                error_json('短信验证码不正确');
            }
        } else {
            error_json('电话号码不正确');
        }
    }
    public function unset_mobile()
    {
        unset($_SESSION['sms_code']);
        unset($_SESSION['sms_mobile']);
        unset($_SESSION['mobile_update_token']);
    }
    function conn_info($arr)
    {
        $alias = isset($arr['alias']) ? $arr['alias'] : '';
        if ($alias == 'wchat') {
            @($_SESSION['connect']['wchat']['access_token'] = wchat::init()->access_token());
            $winfo = wchat::init()->user_info($wchat_uid);
            @($_SESSION['connect']['wchat']['nick'] = $winfo['nick']);
            @($_SESSION['connect']['wchat']['avatar'] = $winfo['avatar']);
            @($_SESSION['connect']['wchat']['sex'] = $winfo['sex']);
        }
        $connrow['nick'] = $_SESSION['connect'][$alias]['nick'];
        $connrow['avatar'] = $_SESSION['connect'][$alias]['avatar'];
        $connrow['sex'] = $_SESSION['connect'][$alias]['sex'];
        if ($_SESSION['connect'][$alias]['openid'] != '') {
            $connrow['open_status'] = '1';
        } else {
            $connrow['open_status'] = '0';
        }
        return $connrow;
    }
    function avatar_save($url)
    {
        $avatar_url = curl::downloadImgaes($url);
        $avatar_name = $this->module('album')->upload_move($avatar_url, 'avatar');
        $result_json = json_decode($avatar_name);
        return $result_json->name;
    }
}